<?php

namespace App\Http\Controllers\Admin;

use App\Models\Menu;
use App\Models\Role;
use App\Models\RoleMenu;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MenusController extends Controller
{

    /**
     * 页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  index()
    {



        return view('admin/menu/index');
    }
    /**
     * 菜单路由
     */
    public function    getAllRoleMenu(Request $request)
    {
        $id = $request->id;
        $menu = $this->menu_role_list(0,$id);

        $list =[];
        foreach ($menu as $key=>$menus){
            $list[]=$menus;
        }

        return $menu;

    }

    /**
     *
     * @param int $pid
     * @param $id
     * @return array
     */
    public function  menu_role_list($pid=0,$id)
    {
//        $role = Role::find($id);

        $role_menu = RoleMenu::where('role_id',$id)->pluck('menu_id')->toArray();

        $list = Menu::where(['p_id'=>$pid])->select('id','title')->get()->toArray();
        $menu = [];

        if(count($list) > 0){
            foreach ($list as $key=>$value){

                if($role_menu) {
                    if (in_array($value['id'], $role_menu)) {

                        $value['checked']=true;
//                        dd($menu);
                    }
                }

                $menu[$key]=$value;

                $children   =  $this->menu_list($value['id']);

                if($children){
                    foreach ($children as $kk=>$child){
                        if($role_menu) {
                            if (in_array($child['id'], $role_menu)) {

                                $menu[$key]['children'][$kk]['checked'] = true;
                            }
                        }

                    }

                    $menu[$key]['children']=$children;

                }

            }
        }

        return $menu;
    }
    /**
     * 创建页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public  function  add()
    {
        $menu = Menu::where('p_id',0)->get();
        return view('admin/menu/add',['menu'=>$menu]);
    }

    /**
     * 菜单路由
     */
    public function    getAllMenu()
    {
        $menu = $this->menu_list();
        $list =[];
        foreach ($menu as $key=>$menus){
            $list[]=$menus;
        }

        return $list;

    }


    public function  menu_list($pid=0)
    {

        $list = Menu::where(['p_id'=>$pid])->select('id','title')->get()->toArray();
        $menu = [];

        if(count($list) > 0){
            foreach ($list as $key=>$value){
                $menu[$key]=$value;
                $children   =  $this->menu_list($value['id']);
                if($children){
                    $menu[$key]['children']=$children;
                }

            }
        }

        return $menu;
    }

    /**
     * 编辑页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $params = $request->only(['menu_id']);

        $validator = \Validator::make($params, [
            'menu_id'=>'required|exists:menus,id',
        ], [
            'menu_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $menu = Menu::where('p_id',0)->get();
        $menuDetail= Menu::find($request->menu_id);
        return view('admin/menu/edit',['menu'=>$menu,'data'=>$menuDetail]);
    }

    /**
     * 权限路由
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  data(Request $request)
    {
        $data = Menu::Name($request->name)->paginate()->toArray();

        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
    }


    /**
     * 路由详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  detail(Request $request)
    {
        $params = $request->only(['menu_id']);
        $validator = \Validator::make($params, [
            'menu_id'=>'required|exists:menus,id',
        ], [
            'menu_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
       $menu = Menu::find($request->menu_id);

       return $this->success($menu);
    }

    /**
     * 新增路由
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function  addMenu(Request $request)
    {
        $params = $request->only(['title','path','p_id','icon','level']);
        $validator = \Validator::make($params, [
            'title'=>'required|string|max:20',
//            'path'=>'required|string|max:20',
            'p_id'=>'required',
//            'icon'=>'required|string|max:20',
            'level'=>'required',
        ], [
            'title.*' => '名称参数错误',
//            'path.*' => '路由参数错误',
            'p_id.*' => '上级参数错误',
//            'icon.*' => '参数错误',
            'level.*' => '等级参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $data=[
            'title'=>$request->title,
            'path'=>$request->path,
            'p_id'=>$request->p_id,
            'icon'=>'',
            'level'=>$request->level
        ];
       $re =  Menu::add($data);
        if($re){
            return $this->success(['msg'=>'保存成功']);
        }
        return $this->error('保存失败');
    }

    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public  function  saveMenu(Request $request)
    {
        $params = $request->only(['menu_id','title','path','p_id','level']);
        $validator = \Validator::make($params, [
            'menu_id'=>'required|exists:menus,id',
            'title'=>'required|string|max:20',
//            'path'=>'required|string|max:20',
            'p_id'=>'required',
//            'icon'=>'required|string|max:20',
            'level'=>'required',
        ], [
            'menu_id.*' => '参数错误',
            'title.*' => '参数错误',
//            'path.*' => '参数错误',
            'p_id.*' => '参数错误',
            'level.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $re = Menu::edit($request->all());
        if($re){
            return $this->success(['msg'=>'保存成功']);
        }
        return $this->error('保存失败');
    }
}
